package com.gzist.blog_system.controller;

import com.gzist.blog_system.domain.result.Result;
import com.gzist.blog_system.service.IQiniuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@RestController
@Api(tags = "文件上传模块")
public class FileUploadController {

    @Resource
    private IQiniuService iQiniuService;

    /*
     * @description:
     *  七牛云存储文件
     * @param: [file]
     **/
    @PostMapping("/upload")
    @ApiOperation("文件上传接口")
    public Result<String> upload(@ApiParam("上传的文件") MultipartFile file) throws IOException {
        //获取文件名
        String originalFilename = file.getOriginalFilename();
        //使其文件名唯一
        String fileName = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        //利用整合阿里云的第三方OSS技术根据文件流以及文件名获取文件在云中存放的地址并将其作为结果进行返回
//        String url = AliOssUtil.uploadFile(fileName, file.getInputStream());
        //使用七牛云的oss来实现文件存储功能
        String url = iQiniuService.uploadFile(file.getInputStream(),fileName);
        return Result.success(url);
    }
}
